package com.dhcc.bpm.modules.bbs.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.common.utils.DateUtils;
import com.dhcc.bpm.common.utils.SafeHtmlUtils;
import com.dhcc.bpm.modules.bbs.entity.Post;
import com.dhcc.bpm.modules.bbs.entity.TopicUser;
import com.dhcc.bpm.modules.bbs.service.IPostService;
import com.dhcc.bpm.modules.bbs.service.ITopicUserService;
import com.dhcc.bpm.modules.bbs.vo.PostVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Base64Util;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName PostController
 * @Description 论坛帖子表 控制器
 * @Author daina
 * @Date 2020/4/13 15:55
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@RestController
@AllArgsConstructor
@RequestMapping("bpm-bbs/post")
@Api(value = "帖子表", tags = "接口")
public class PostController extends BladeController {

	private IPostService postService;
	private ITopicUserService topicUserService;
	/**
	 * @Description 详情
	 * @Author daina
	 * @Date 14:13 2020/4/13
	 * @param post
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.bbs.entity.Post>
	 **/
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入post")
	@ApiLog("查看帖子详情")
	@Log(title = "查看帖子详情",businessType = BusinessType.SELECT)
	public R<Post> detail(Post post) {
		Post detail = postService.getOne(Condition.getQueryWrapper(post));
		return R.data(detail);
	}

	/**
	 * @Description 分页
	 * @Author daina
	 * @Date 14:13 2020/4/13
	 * @param post
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.bbs.entity.Post>>
	 **/
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入post")
	@ApiLog("查看帖子详情")
	@Log(title = "查看帖子详情",businessType = BusinessType.SELECT)
	public R<IPage<Post>> list(Post post, Query query) {
		IPage<Post> pages = postService.page(Condition.getPage(query), Condition.getQueryWrapper(post));

		return R.data(pages);
	}

	/**
	 * @Description 查询帖子相关信息列表 自定义分页
	 * @Author daina
	 * @Date 14:13 2020/4/13
	 * @param post
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.bbs.vo.PostVO>>
	 **/
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入post")
	@ApiLog("查询帖子相关信息列表")
	@Log(title = "查询帖子相关信息列表",businessType = BusinessType.SELECT)
	public R<IPage<PostVO>> page(PostVO post, Query query) {
		IPage<PostVO> pages = postService.selectPostPage(Condition.getPage(query), post);
		BladeUser user = AuthUtil.getUser();
		List<PostVO> postLists = pages.getRecords();
		List<PostVO> newPostLists = new ArrayList<PostVO>();
		if(!user.getAccount().equals("admin")){
			if (postLists.size() > 0){
				for (PostVO postVO : postLists){
					TopicUser topicUser = new TopicUser();
					topicUser.setBbsTopicId(postVO.getBbsTopicId());
					List<TopicUser> topicUserList = topicUserService.listTopicUser(topicUser);
					if (topicUserList.size() > 0) {
						for(TopicUser topicUserVO : topicUserList){
							if (topicUserVO.getOpcUserId().equals(user.getUserId())) {
								newPostLists.add(postVO);
							}
						}
					}
				}
			}
			pages.setTotal(newPostLists.size());
			pages.setRecords(newPostLists);
		}
		return R.data(pages);
	}

	/**
	 * @Description 新增
	 * @Author daina
	 * @Date 14:14 2020/4/13
	 * @param post
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入post")
	@ApiLog("新增帖子")
	@Log(title = "新增帖子",businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody Post post) {
		String safeHtml = SafeHtmlUtils.getSafeHtml(post.getPostContent());
		post.setPostContent(safeHtml);
		return R.status(postService.save(post));
	}

	/**
	 * @Description 修改
	 * @Author daina
	 * @Date 14:14 2020/4/13
	 * @param post
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入post")
	@ApiLog("修改帖子")
	@Log(title = "修改帖子",businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody Post post) {
		String safeHtml = SafeHtmlUtils.getSafeHtml(post.getPostContent());
		post.setPostContent(safeHtml);
		return R.status(postService.updateById(post));
	}

	/**
	 * @Description 新增或修改
	 * @Author daina
	 * @Date 14:14 2020/4/13
	 * @param post
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入post")
	@ApiLog("新增或修改帖子")
	@Log(title = "新增或修改帖子",businessType = BusinessType.INSERT)
	public R submit(@Valid @RequestBody Post post) {
		String postContent = Base64Util.decode(post.getPostContent());
		post.setPostContent(postContent);
		BladeUser user = AuthUtil.getUser();
		post.setCheckTime(DateUtils.getNowDate());
		post.setCheckUser(user.getUserId());
		String safeHtml = SafeHtmlUtils.getSafeHtml(post.getPostContent());
		post.setPostContent(safeHtml);
		return R.status(postService.saveOrUpdate(post));
	}

	
	/**
	 * @Description 删除
	 * @Author daina
	 * @Date 14:14 2020/4/13
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	@ApiLog("删除帖子")
	@Log(title = "删除帖子",businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(postService.removeByIds(Func.toLongList(ids)));
	}

	
}
